<?php
//update your name upcoming names and publish it's your day topic
//run at 23:55 on Sunday, Tuesday and Thursday
//poster_id and names and topic_category_id still need to be changed!!

$now_weekday=date('w',time());

switch($now_weekday){
    case 0:
    case 1:
        $upcoming_date=strtotime('Monday this week');
        break;
    case 2:
    case 3:
        $upcoming_date=strtotime('Wednesday this week');
        break;
    case 4:
    case 5:
    case 6:
        $upcoming_date=strtotime('Friday this week');
        break;
}

$now_time=$upcoming_date;

$query=$this->db->query("SELECT name_id, name FROM " . DB_PREFIX . "community_yourday WHERE being_displayed='0' AND used='0' AND being_used='0' AND being_polled='0' ORDER BY nominate_count DESC LIMIT 0,1");

if($query->row){
    #####add new name in the upcoming name list#####

    echo 'add new name cron run!';
    echo $query->row['name'];

    //display new name in the upcoming name list
    $this->db->query("UPDATE " . DB_PREFIX . "community_yourday SET upcoming_date='" .$now_time . "', being_displayed='1' WHERE name_id='" . $query->row['name_id'] . "'");

    #####post topic for the new name####
    echo 'post topic for current name cron run!';

    $time=$now_time;

    //post a topic
    $this->db->query("INSERT INTO " . DB_PREFIX . "community_topics SET forum_id='" . (int)$this->config->get('your_day_forum_id') . "', topic_your_day='1', topic_approved='1', topic_polled='0', topic_category_id='18', topic_title='" . $this->db->escape(date('F jS, Y',$time) . '-' . ucwords($query->row['name'])) . "', topic_poster_id='3', topic_time='" . $time . "', topic_first_poster_name='xiaoming', topic_last_poster_id='1', topic_last_poster_name='xiaoming', topic_last_post_subject='" . $this->db->escape(date('F jS, Y',$time) . '-' . ucwords($query->row['name'])) . "', topic_last_post_time='" . $time . "'");

    $topic_id=$this->db->getLastId();

    $this->db->query("INSERT INTO " . DB_PREFIX . "community_posts SET forum_id='" . (int)$this->config->get('your_day_forum_id') . "', topic_id='" . (int)$topic_id . "', poster_id='3', poster_ip='" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', post_time='" . $time . "', post_approved='1', post_username='xiaoming', post_subject='" . $this->db->escape(date('F jS, Y',$time) . '-' . ucwords($query->row['name'])) . "', post_text='" . $this->db->escape('Hi, ' . ucwords($query->row['name']) . "! Come and claim your freebie!\n\n[b][size=150]Note:If your name is not " . ucwords($query->row['name']) . " and you try to claim the freebie, you will get banned permanently and your name will get blacklisted by us forever! Than you for your cooperation and helping to make this promotion better.[/size][/b]") . "'");

    $post_id=$this->db->getLastId();

    $this->db->query("UPDATE " . DB_PREFIX . "community_topics SET topic_first_post_id='" . (int)$post_id . "', topic_last_post_id='" . (int)$post_id . "' WHERE topic_id='" . (int)$topic_id . "'");

    //update forum data
    $this->db->query("UPDATE " . DB_PREFIX . "community_forum SET forum_posts=forum_posts+1, forum_topics=forum_topics+1, forum_last_post_id='" . $post_id . "', forum_last_poster_id='3', forum_last_post_subject='" . $this->db->escape(date('F jS, Y',$time) . '-' . ucwords($query->row['name'])) . "', forum_last_post_time='" . $time . "', forum_last_poster_name='xiaoming' WHERE forum_id='" . (int)$this->config->get('your_day_forum_id') . "'");

    $this->db->query("UPDATE " . DB_PREFIX . "community_users SET user_topics=user_topics+1, user_posts=user_posts+1, user_lastpost_time='" . (int)$time . "' WHERE user_id='3'");

    $this->db->query("UPDATE " . DB_PREFIX . "setting SET `value`=`value`+1 WHERE `key`='total_topics'");
    $this->db->query("UPDATE " . DB_PREFIX . "setting SET `value`=`value`+1 WHERE `key`='total_posts'");

    //clear forum sql cache
    $encrypt_data=md5($this->config->get('your_day_forum_id'));
    $this->sql_cache->delete('viewforum.topictotal.' . $encrypt_data);
    $this->sql_cache->delete('viewforum.topics.' . $encrypt_data);
    $this->sql_cache->delete('community.foruminfo.');

    //update name
    $this->db->query("UPDATE " . DB_PREFIX . "community_yourday SET forum_id='" . (int)$this->config->get('your_day_forum_id') . "', topic_id='" . (int)$topic_id . "', topic_first_post_id='" . (int)$post_id . "', being_used='1' WHERE name_id='" . $query->row['name_id'] . "'");

    //send emails to people called that name
    $email_query=$this->db->query("SELECT email FROM " . DB_PREFIX . "community_yourday_email WHERE name_id='" . $query->row['name_id'] . "'");

    $mail_num=0;

    foreach($email_query->rows as $email_address){
        if($mail_num==499){
            $subject =  'Emails sent to It\'s Your Day nominators reach 499!';

            $message = 'Time is:' . date('Y-m-d H:i:s') . "\n";

            $mail = new Mail();
            $mail->protocol = $this->config->get('config_mail_protocol');
            $mail->parameter = $this->config->get('config_mail_parameter');
            $mail->hostname = $this->config->get('config_smtp_host');
            $mail->username = $this->config->get('config_smtp_username');
            $mail->password = $this->config->get('config_smtp_password');
            $mail->port = $this->config->get('config_smtp_port');
            $mail->timeout = $this->config->get('config_smtp_timeout');
            $mail->setTo('marcelwu@126.com');
            $mail->setFrom($this->config->get('config_community_email'));
            $mail->setSender($this->config->get('config_name') . "\40Community Emergency Team");
            $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
            $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
            $mail->send();

            break;
        }

        $subject =  'Freebies FROM Micbus.com - ' . ucwords($query->row['name']) . '\'s Day Has Come!';

        $message = date('F jS, Y',$time) . "is\40" . ucwords($query->row['name']) . "'s Day! Now you can try to claim your freebie at:\n";
        $message .= $this->url->link('community/viewtopic', 'fid=' . $this->config->get('your_day_forum_id') . '&tid=' . $topic_id, 'SSL') . "\n\n";
        $message .= "Cheers\n";
        $message .= $this->config->get('config_name') . "\40Community Manager Team";

        $mail = new Mail();
        $mail->protocol = $this->config->get('config_mail_protocol');
        $mail->parameter = $this->config->get('config_mail_parameter');
        $mail->hostname = $this->config->get('config_smtp_host');
        $mail->username = $this->config->get('config_smtp_username');
        $mail->password = $this->config->get('config_smtp_password');
        $mail->port = $this->config->get('config_smtp_port');
        $mail->timeout = $this->config->get('config_smtp_timeout');
        $mail->setTo($email_address['email']);
        $mail->setFrom($this->config->get('config_community_email'));
        $mail->setSender($this->config->get('config_name') . "\40Community Manager Team");
        $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
        $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
        $mail->send();

        $mail_num++;
        sleep(0.1);
    }
}

//clear your name cache
$this->sql_cache->delete('community.upcomingnames');
?>